package cn.bdqn.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

//饿汉模式
public class Singleton {
	//类加载的时候，自行进行初始化操作
	private static Singleton singleton = new Singleton();
	private static Properties properties;
	
	//私有构造器-读取配置文件
	private Singleton(){
		String configFile = "database.properties";
		properties = new Properties();
		//通过classpath找资源
		InputStream is = ConfigManager.class.getClassLoader().getResourceAsStream(configFile);
		try {
			properties.load(is);
			is.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static Singleton getInStance(){
		return singleton;
	}
	
	public String getValue(String key){
		return properties.getProperty(key);
	}
	/*
	//饿汉模式（变种）
	private static Singleton singleton;
	static{
		singleton = new Singleton();
		System.out.println("static ====== " + singleton.toString());
	}
	private Singleton(){}
	
	public static Singleton getInstance(){
		System.out.println("getInstance======");
		return singleton;
	}
	*/
	public static void test(){
		System.out.println("test======" + singleton.toString());
	}
	
}
